﻿using System.Collections.Generic;
using System.Linq;
using AbcCorp.DTO;
using DAO.EntityModel;

namespace AbcCorp.DAO
{
    /// <summary>
    /// Lớp phương thức thanh toán
    /// </summary>
    public class PaymentMethodDao
    {
        #region Variable
        /// <summary>
        /// Khai báo tượng entity model
        /// </summary>
        private readonly ABCCorpEntities _abcCorpEntities = new ABCCorpEntities();
        #endregion

        #region Methods
        /// <summary>
        /// Lấy phương thức thanh toán dựa vào mã phương thức
        /// </summary>
        /// <param name="methodid">Mã phương thức</param>
        public PaymentMethodDto GetMethod(int methodid)
        {
            var method = from mthd in _abcCorpEntities.PaymentMethods.Where(m => m.MethodID.Equals(methodid))
                         select mthd;
            var paymentMethod = method.SingleOrDefault();
            if (paymentMethod == null) return null;
            var methodDto = new PaymentMethodDto();
            CopyObject<PaymentMethod, PaymentMethodDto>.CopyProperties(method.SingleOrDefault(), methodDto);
            return methodDto;
        }

        /// <summary>
        /// Lấy tất cả các phương thức thanh toán
        /// </summary>
        /// <returns>PaymentMethodDto</returns>
        public IList<PaymentMethodDto> GetMethods()
        {
            IList<PaymentMethodDto> paymentMethods = new List<PaymentMethodDto>();
            
            var methods = from mthd in _abcCorpEntities.PaymentMethods
                          select mthd;
            if (methods.FirstOrDefault() == null) return null;
            foreach (var method in methods)
            {
                var methodDto = new PaymentMethodDto();
                CopyObject<PaymentMethod, PaymentMethodDto>.CopyProperties(method, methodDto);
                paymentMethods.Add(methodDto);
            }
            return paymentMethods;
        }
        #endregion
    }
}
